package cn.edu.gduf.luowowo.website.web.controller;

import cn.edu.gduf.luowowo.article.domain.Destination;
import cn.edu.gduf.luowowo.article.service.IDestinationService;
import cn.edu.gduf.luowowo.cache.service.IHotelRedisService;
import cn.edu.gduf.luowowo.common.query.QueryObject;
import cn.edu.gduf.luowowo.common.util.AjaxResult;
import cn.edu.gduf.luowowo.hotel.domain.Hotel;
import cn.edu.gduf.luowowo.hotel.query.HotelQueryObject;
import cn.edu.gduf.luowowo.hotel.service.IHotelService;
import cn.edu.gduf.luowowo.hotel.service.IHotelThemeService;
import cn.edu.gduf.luowowo.member.domain.UserInfo;
import cn.edu.gduf.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/hotel")
public class HotelController {
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IHotelService hotelService;
    @Reference
    private IHotelThemeService hotelThemeService;
    @Reference
    private IHotelRedisService hotelRedisService;

    @RequestMapping("")
    public String hotelIndex(Model model) {
        //dest 查询国内的部分城市用于显示在搜索框
        List<Destination> destinations = destinationService.queryDestsForChinaCity();
       /* Destination d = new Destination();
        d.setId(3261L);
        d.setName("广州");
        destinations.add(d);*/
        model.addAttribute("dest", destinations);
        //hotelTags 显示hotel的主题标签
        model.addAttribute("hotelTags", hotelThemeService.list());
        //hotelCity 查询特价酒店的城市。将酒店按照价格排序，拿去重后的排序前六的酒店关联的城市
        model.addAttribute("hotelCity", destinationService.queryForHotelCity());
        return "hotel/hotel";
    }

    @RequestMapping("/theme")
    public String theme(Model model, Long id){
        //list
        model.addAttribute("list", destinationService.queryByHotelThemeId(id));
        return "hotel/hotelTpl";
    }

    @RequestMapping("/theme1")
    public String theme1(Model model, Long cityId){
        //把这里的cityId当作是目的地id就可以。根据目的地id查询相关的酒店信息
        //hotelScore
        model.addAttribute("hotelScore", hotelService.querySimpleByCityId(cityId));
        return "hotel/hotelTpl1";
    }

    @RequestMapping("/h")
    public String HotelsByDest(Model model, Long id, @ModelAttribute("qo") HotelQueryObject qo){
        if (id == null){
            id = 3261L;
        }
        model.addAttribute("dest", destinationService.get(id));
        //这里的id是某个目的地的id 根据目的地id查询该目的地所有的酒店
        //hotel
        model.addAttribute("hotel", hotelService.queryByDestId(id));
        return "hotel/dingjiudian";
    }

    @RequestMapping("/detail")
    public String detail(Model model, Long id){
        model.addAttribute("detail", hotelService.get(id));
        return "hotel/detail";
    }

    @GetMapping("/{id}")
    public String hotelSearch(Model model, @PathVariable Long id){
        if (id == null){
            id = 3261L;
        }
        model.addAttribute("dest", destinationService.get(id));
        //这里的id是某个目的地的id 根据目的地id查询该目的地所有的酒店
        //hotel
        model.addAttribute("hotel", hotelService.queryByDestId(id));
        return "hotel/dingjiudian";
    }

    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(Long hotelId, @UserParam UserInfo userInfo){
        if (userInfo == null) {
            AjaxResult result = new AjaxResult(false, "登录后操作");
            result.setCode(102);
            return result;
        }
        if (hotelId == null){
            return new AjaxResult(false, "非法操作");
        }
        //判断该用户是否收藏过该酒店
        Long userId = userInfo.getId();
        boolean flag = hotelRedisService.favor(userId, hotelId);
        return new AjaxResult(flag, flag ? "收藏成功" : "取消收藏");
    }

}
